package com.foo.DataProvider;

import java.sql.Date;
import java.sql.Timestamp;

import javax.ejb.Stateless;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.foo.SnapshotDataProvider.*;
import com.foo.SpatialDataProvider.*;
import com.foo.TemporalDataProvider.TemporalDataProvider;
import com.foo.AnalyzedDataProvider.*;
import com.foo.RealtimeDataProvider.RealtimeDataProvider;
import com.foo.HistoricalData.*;

import com.foo.hibernate.*;
import com.foo.AllRealtimeDataProvider.*;

/**
 * Session Bean implementation class Provider
 */
@Stateless
public class Provider implements ProviderRemote, ProviderLocal {

    /**
     * Default constructor. 
     */
    public Provider() 
    {
    	System.out.println("In Provider class!");
    }

	public void getData(String sorttype,String value, Timestamp timestamp, Date fromdate, Date todate)
	{
		try
		{
		ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
		System.out.println("Get Data in Provider Called!");
		//if(sorttype!=null && value==null && fromdate!=null && todate!=null)
		if(timestamp!=null)
		{
			ProviderMain provider = (ProviderMain) context.getBean("Provider1");  
			System.out.println("In Realtime clause!");
			RealtimeDataProvider Object = (RealtimeDataProvider) context.getBean("RealtimeDataProvider");  
			provider.setProvider(Object);
			provider.getData(sorttype,value,timestamp,fromdate,todate);
		}
		else if(value == null)	
		{
			if(sorttype.equalsIgnoreCase("AllRealTimeData"))
			{
				ProviderMain provider = (ProviderMain) context.getBean("Provider1");  
				System.out.println("In AllRealtime clause!");
				AllRealTimeData Object = (AllRealTimeData) context.getBean("AllRealTimeDataProvider");  
				provider.setProvider(Object);
				provider.getData(sorttype,value,timestamp,fromdate,todate);
			}
			else if(sorttype.equalsIgnoreCase("TopVolume") || sorttype.equalsIgnoreCase("TopIncr") || sorttype.equalsIgnoreCase("TopDecr"))
			{
				
				ProviderMain provider = (ProviderMain) context.getBean("Provider1");  
				System.out.println("In Analyzed clause!");
				
				AnalyzedDataProvider analyzedObject = (AnalyzedDataProvider) context.getBean("AnalyzedDataProvider");  
				//AnalyzedHibernate analyzedObject = (AnalyzedHibernate) context.getBean("AnalyzedHibernate");
				
				provider.setProvider(analyzedObject);
				provider.getData(sorttype,value,timestamp,fromdate,todate);
			}
			else
			{
				System.out.println("Temporal Provider clause Called!");
				ProviderMain provider = (ProviderMain) context.getBean("Provider1");  
				System.out.println("In Temporal clause!");
				TemporalDataProvider temporalObject = (TemporalDataProvider) context.getBean("TemporalDataProvider");  
				provider.setProvider(temporalObject);
				provider.getData(sorttype,value,timestamp,fromdate,todate);
			}
		}
		else if(sorttype!=null && value!=null)
		{
			if(sorttype.equalsIgnoreCase("Country") || sorttype.equalsIgnoreCase("Sector") ||sorttype.equalsIgnoreCase("Industry") ||sorttype.equalsIgnoreCase("State"))
				{
				ProviderMain provider = (ProviderMain) context.getBean("Provider1");  
				System.out.println("In Spatial clause!");
				SpatialDataProvider Object = (SpatialDataProvider) context.getBean("SpatialDataProvider");  
				provider.setProvider(Object);
				provider.getData(sorttype,value,timestamp,fromdate,todate);
				}
			else if(value.equalsIgnoreCase("historical"))
			{
				ProviderMain provider = (ProviderMain) context.getBean("Provider1");  
				System.out.println("In Historical clause!");
				HistoricDataProvider Object = (HistoricDataProvider) context.getBean("HistoricDataProvider");  
				provider.setProvider(Object);
				provider.getData(sorttype,value,timestamp,fromdate,todate);
			}
			else
			{
				ProviderMain provider = (ProviderMain) context.getBean("Provider1");  			
				System.out.println("In Snapshot clause!");
				SnapshotDataProvider snapshotObject = (SnapshotDataProvider) context.getBean("SnapshotDataProvider");  
				provider.setProvider(snapshotObject);
				provider.getData(sorttype,value,timestamp,fromdate,todate);
			}
		}
		}
		catch(Exception e)
		{
			
		}
	}
}
